package std.bank;

import java.io.IOException;
import java.net.UnknownHostException;

public class AccountRPC implements Account 
{
	ClientStub client;
	
	public AccountRPC(String name, double balance) throws UnknownHostException, IOException
	{
		Object[] parameterValues = {name, balance};
		Class<?>[] parameterTypes = {name.getClass(), double.class};
		client = new ClientStub(parameterTypes, parameterValues);
	}
	
	@Override
	public synchronized void deposit(double amount)
	{
		String methodName = "deposit";
		Object[] parameterValues = {amount};
		Class<?>[] paramaterTypes = {double.class};
		client.performMethodCall(methodName, paramaterTypes, parameterValues);
	}

	@Override
	public double getBalance() 
	{
		String methodName = "getBalance";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		String balanceString = (String)client.performMethodCall(methodName, paramaterTypes, parameterValues);
		Double balanceDouble = new Double(balanceString);
		return balanceDouble;
	}

	@Override
	public String getName() 
	{
		String methodName = "getName";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		return (String)client.performMethodCall(methodName, paramaterTypes, parameterValues);
	}

	@Override
	public synchronized void withdraw(double amount) 
	{
		if(amount > 0)
		{
			String methodName = "withdraw";
			Object[] parameterValues = {amount};
			Class<?>[] paramaterTypes = {double.class};
			client.performMethodCall(methodName, paramaterTypes, parameterValues);
		}
	}
	
	public String toString()
	{
		String methodName = "toString";
		Object[] parameterValues = {};
		Class<?>[] paramaterTypes = {};
		return (String)client.performMethodCall(methodName, paramaterTypes, parameterValues);
	}

}
